---
title: "Innovation_model specification_pid_FG and MR"
author: ""
date: "12/14/2020"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```

### Ordered logistic regression ###
```{r}
require(foreign)
require(ggplot2)
require(MASS)
require(Hmisc)
require(reshape2)
library(readr)
inno_data <- inno_data <- read_csv("inno_data.csv")
inno_data$needs_support_ordinal <- as.factor(inno_data$needs_support)
```
```{r}
#install.packages("GGally")
df_model <- subset(inno_data, 
                   select = c("AGE", "female", "black", "hispanic", "own",  "pid", "ideo", "college",
                   "married", "income_gt50k" , "region"))

library(GGally)
ggcorr(df_model, method = c("pairwise", "pearson"),
  nbreaks = NULL, digits = 2, low = "#3B9AB2",
  mid = "#EEEEEE", high = "#F21A00",
  geom = "tile", label = FALSE,
  label_alpha = FALSE)

#ggcorr(df_model,
#    nbreaks = 6,
#    low = "steelblue",
#    mid = "white",
#    high = "darkred",
#    geom = "circle")
```


```{r}
m <- polr(needs_support_ordinal ~ AGE + female + black + hispanic + own  + factor(pid) + factor(ideo) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m)
```
```{r}
m1 <- polr(needs_support_ordinal ~ AGE + female + black + hispanic + own  + factor(pid) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m1)
```
```{r}
m2 <- polr(needs_support_ordinal ~ AGE + female + black + hispanic + own  + factor(ideo) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m2)
```
## store coefficient table
\newpage
```{r}
ctable <- coef(summary(m1))
print(ctable)
knitr::kable(broom::tidy(m1))
```

## calculate and store p values
```{r}
p <- pnorm(abs(ctable[, "t value"]), lower.tail = FALSE) * 2
```

## combined table
```{r}
ctable <- cbind(ctable, "p value" = p)
print(ctable)

```

## Get Confidence Intervals (default method gives profiled CIs)
```{r}
ci <- confint(m1)
print(ci)

```

## odds ratios
```{r}
exp(coef(m1))
```


## OR and CI
```{r}
exp(cbind(OR = coef(m1), ci))
print(ctable)

```

Part 2.2: Analysis for support for redistributive treatment funding (ability_support)
### Ordered logistic regression ###
```{r}
require(foreign)
require(ggplot2)
require(MASS)
require(Hmisc)
require(reshape2)

inno_data$ability_support_ordinal <- as.factor(inno_data$ability_support)
```


```{r}
m_ability <- polr(ability_support_ordinal ~ AGE + female + black + hispanic + own  + factor(pid) + factor(ideo) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m_ability)
```

```{r}
m_ability2 <- polr(ability_support_ordinal ~ AGE + female + black + hispanic + own  + factor(pid) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m_ability2)
```
```{r}
m_ability3 <- polr(ability_support_ordinal ~ AGE + female + black + hispanic + own + factor(ideo) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m_ability3)
```

## store coefficient table
```{r}
ctable_ability <- coef(summary(m_ability2))
#print(ctable_ability)
knitr::kable(broom::tidy(m_ability2))
```

## calculate and store p values
```{r}
p_ability <- pnorm(abs(ctable_ability[, "t value"]), lower.tail = FALSE) * 2
```

## combined table
```{r}
ctable_ability <- cbind(ctable_ability, "p value" = p)
print(ctable_ability)

```

## Get Confidence Intervals (default method gives profiled CIs)
```{r}
ci_ability <- confint(m_ability2)
print(ci_ability)

```

## odds ratios
```{r}
exp(coef(m_ability2))
```


## OR and CI
```{r}
exp(cbind(OR = coef(m_ability2), ci))
print(ctable_ability)
```


Part 2.3: Analysis for support for building of a treatment facility nearby (distance_support)
### Ordered logistic regression ###
```{r}
require(foreign)
require(ggplot2)
require(MASS)
require(Hmisc)
require(reshape2)

inno_data$dist_support_ordinal <- as.factor(inno_data$dist_support)
```


```{r}
m_dist <- polr(dist_support_ordinal ~ AGE + female + black + hispanic + own  + factor(pid) + factor(ideo) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m_dist)
```

```{r}
m_dist2 <- polr(dist_support_ordinal ~ AGE + female + black + hispanic + own  + factor(pid) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m_dist2)
```

```{r}
m_dist3 <- polr(dist_support_ordinal ~ AGE + female + black + hispanic + own  + factor(ideo) + college + married+ income_gt50k + factor(region), data=inno_data, Hess=TRUE)
summary(m_dist3)
```

## store coefficient table
```{r}
ctable_dist <- coef(summary(m_dist2))
#print(ctable_dist)
knitr::kable(broom::tidy(m_dist2))
```

## calculate and store p values
```{r}
p_dist <- pnorm(abs(ctable_dist[, "t value"]), lower.tail = FALSE) * 2
```

## combined table
```{r}
ctable_dist <- cbind(ctable_dist, "p value" = p)
print(ctable_dist)

```

## Get Confidence Intervals (default method gives profiled CIs)
```{r}
ci_dist <- confint(m_dist2)
print(ci_dist)

```

## odds ratios
```{r}
exp(coef(m_dist2))
```


## OR and CI
```{r}
exp(cbind(OR = coef(m_dist2), ci))
print(ctable_dist)

```

